<template>
  <view class="flex-col">

    <car-info :value="form"></car-info>

    <view class="page-wrap" :class="canEdit?'bottom115':''">

      <view class="lineBottom10 page-section">
        <uni-section title="GPS排查信息反馈" type="line"/>
      </view>

      <uni-forms ref="form" :value="form" labelWidth="auto" :labelPosition="canEdit?'top':'left'">

        <uni-forms-item name="gpsState" label="GPS状态:">
          <uni-easyinput disabled v-if="canEdit" type="text" v-model="form.gpsState"/>
          <text v-else class="text-value">{{ form.gpsState|filterNull }}</text>
        </uni-forms-item>

        <uni-forms-item name="dismantleTime" label="状态持续时长:">
          <uni-easyinput disabled v-if="canEdit" type="text" v-model="form.dismantleTime"/>
          <text v-else class="text-value">{{ form.dismantleTime|filterNull }}</text>
        </uni-forms-item>

        <uni-forms-item name="offLineCause" label="离线原因:">
          <uni-easyinput disabled v-if="canEdit" type="text" v-model="form.offLineCause"/>
          <text v-else class="text-value">{{ form.offLineCause|filterNull }}</text>
        </uni-forms-item>

        <uni-forms-item name="alarmType" label="报警类型:">
          <uni-easyinput disabled v-if="canEdit" type="text" v-model="form.alarmType"/>
          <text v-else class="text-value">{{ form.alarmType|filterNull }}</text>
        </uni-forms-item>

        <uni-forms-item name="alarmTime" label="报警时间:">
          <uni-easyinput disabled v-if="canEdit" type="text" v-model="form.alarmTime"/>
          <text v-else class="text-value">{{ form.alarmTime|filterNull }}</text>
        </uni-forms-item>

        <uni-forms-item :required="canEdit" name="feedbackCause" label="反馈原因:" :rules="[{
                                         required: true,
                                         errorMessage: '请选择反馈原因',
                                         validateTrigger: 'bind'
                                       }]">
          <zxz-uni-data-select
            v-model="form.feedbackCause"
            dataKey="label"
            dataValue="value"
            v-if="canEdit"
            :localdata="dict.type.gps_feedback_reason"></zxz-uni-data-select>
          <dict-tag v-else
                    :options="dict.type.gps_feedback_reason"
                    :value="form.feedbackCause"/>
        </uni-forms-item>

        <uni-forms-item :required="canEdit" name="isTroubleshoot" label="是否继续排查:" :rules="[{
                                         required: true,
                                         errorMessage: '请选择是否继续排查',
                                         validateTrigger: 'bind'
                                       }]">
          <zxz-uni-data-select
            v-model="form.isTroubleshoot"
            dataKey="name"
            dataValue="code"
            v-if="canEdit"
            :localdata="[{name:'否', code: '0'}, {name:'是', code:'1'}]"></zxz-uni-data-select>
          <text v-else class="text-value">{{ 0 == form.isTroubleshoot ? "否" : 1 == form.isTroubleshoot? "是" : "--" }}</text>
        </uni-forms-item>

        <uni-forms-item name="imageOss" label="上传照片:">
        </uni-forms-item>
        <image-common :disabled="!canEdit" :up-type="2" v-model="form.imageOss"></image-common>

        <uni-forms-item name="remark" label="备注:">
          <uni-easyinput v-if="canEdit" type="textarea" maxlength="200" v-model="form.remark"
                         placeholder="请输入备注"/>
          <text v-else class="text-value">{{ form.remark|filterNull }}</text>
        </uni-forms-item>

      </uni-forms>

    </view>

    <exam-flow v-if="!canEdit && form.flowId" :instanceId="form.flowId">
    </exam-flow>
    <view class="button-container" v-if="canEdit">
      <button class="btn-item" @click="submit">提 交</button>
    </view>
  </view>
</template>

<script>

import CarInfo from "@/pages/CarPage/gps/components/carInfo.vue";
import {carWorkOrderInfo, getCarGpsTroubleshoot, carGpsTroubleshoot, gpsSimInfo} from "@/api/gps/gpsList"
import UniSection from "@/uni_modules/uni-section/components/uni-section/uni-section.vue";

export default {
  components: {UniSection, CarInfo},
  dicts: ["quota_rent_type", "gps_feedback_reason"],
  data() {
    return {
      form: {},
      state: 'write',
      carIds: null,
    }
  },
  computed: {
    canEdit() {
      return 'write' === this.state;
    }
  },
  onLoad(op) {
    console.log('op', op);
    this.state = op?.state ?? 'write';
    this.carIds = op?.carIds ?? null;

	let title = this.canEdit?'排查工单申请':'排查工单详情';
    if (this.carIds) {
      this.getOldData();
    }
    uni.setNavigationBarTitle({
      title
    })
  },
  methods: {
    getOldData() {
      carWorkOrderInfo({carIds: this.carIds, workOrderType: 17}).then(res => {
        let intentData = res.data[0];
        console.log('接到', intentData)

        if (!this.isEmpty(intentData)) {
          if (!this.isEmpty(intentData.businessDataId)) {
            //提报过，请求详情
            this.initData(intentData.businessDataId)
          } else {
            this.form = {...this.form, ...intentData}
          }
        }
      })
    },
    initData(id) {
      uni.showLoading();
      getCarGpsTroubleshoot(id)
        .then((res) => {
          if (res.code == 200) {
            let data = res.data;
            this.form = {...this.form, ...data}
          }
        })
        .finally(() => {
          uni.hideLoading();
        });
    },

    submit() {
      this.form.contractNo = this.form.contractReportNumber;
      this.form.purchaseNo = this.form.procurementReportNumber;
      this.form.workId = this.form.id;

      this.$refs.form.validate().then(res => {
        uni.showLoading();
        carGpsTroubleshoot(this.form).then(response => {
          uni.hideLoading();
          uni.$emit("refreshGps", true);
          this.$modal.msgSuccess("提交成功");
          setTimeout(() => {
            this.$tab.navigateBack();
          }, 1000)
        }).finally(f => {
			setTimeout(() => {
			  uni.hideLoading();
			}, 1000)
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
